package com.atguigu.lease.web.admin.service.impl;


import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.redis.RedisConstant;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.utils.JwtUtil;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.service.LoginService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.login.CaptchaVo;
import com.atguigu.lease.web.admin.vo.login.LoginVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wf.captcha.SpecCaptcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SystemUserService systemUserService;

    @Override
    public CaptchaVo customCaptcha() {
        //生成验证码图片和正确验证码
        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
        //验证码的内容类型: 数字 | 字母
        specCaptcha.setCharType(SpecCaptcha.TYPE_DEFAULT);
        //获取验证码图片
        //todo: base64是一种转码和解码工具,可以将任何内容转成字符串!
        //todo: 1. 怕乱码的字符串,转成base64  2. 非字符串想要在json中携带,可以转成base64
        // <img src="base64字符串"
        String imageStr = specCaptcha.toBase64();
        //正确的掩码
        String text = specCaptcha.text();
        String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID().toString().replaceAll("-", "");
        redisTemplate.opsForValue().set(key, text, RedisConstant.APP_LOGIN_CODE_RESEND_TIME_SEC, TimeUnit.SECONDS);
        CaptchaVo captchaVo = new CaptchaVo(imageStr, key);
        return captchaVo;
    }

    @Override
    public String customLogin(LoginVo loginVo) {
        //1. 验证码相关 (验证码是否过期 | 验证码是否正确)
        String captchaKey = loginVo.getCaptchaKey();
        String redisCode = (String) redisTemplate.opsForValue().get(captchaKey);
        if (ObjectUtils.isEmpty(redisCode)) {
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
        if (!redisCode.equalsIgnoreCase(loginVo.getCaptchaCode())) {
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }
        //2.检查账号
        LambdaQueryWrapper<SystemUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SystemUser::getUsername, loginVo.getUsername());
        SystemUser user = systemUserService.getOne(lambdaQueryWrapper);
        if (user == null) {
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }
        //3.检测密码
        if (!user.getPassword().equals(DigestUtils.md5Hex(loginVo.getPassword()))) {
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }
        //4.检测可用性
        if(user.getStatus() == BaseStatus.DISABLE){
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }
        //5.生成token返回给客户端
        String token = JwtUtil.createToken(user.getId(), user.getUsername());
        return token;
    }
}
